﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MarketOnlineDTO;
using MarketOnlineBUS;

public partial class Cart : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        lbTrangThai.Text = "";

        if (!IsPostBack)
        {
            if (Request.QueryString["bestbuy"] != null)
            { 
                //Lấy mã món
                int MaMon = int.Parse(Request.QueryString["bestbuy"].ToString());
                List<ThanhPhanDTO> listThanhPhan = new List<ThanhPhanDTO>();
                listThanhPhan = new ThanhPhanBUS().LayDanhSachThanhPhan(MaMon);

                foreach (ThanhPhanDTO item in listThanhPhan)
                {

                    List<GioHangDTO> list = new List<GioHangDTO>();
                    list = (List<GioHangDTO>)Session["Cart"];
                    GioHangBUS bus = new GioHangBUS();

                    if (bus.KiemTra(list, item.MaSP) == false)
                    {
                        list = bus.ThemSanPhamGioHang(list, item.MaSP);
                        Session["Cart"] = list;
                    }
                }

            }

            if (Request.QueryString["id"] != null)
            {
                //Lấy ConString
                int MaSP = int.Parse(Request.QueryString["id"].ToString());
                List<GioHangDTO> list = new List<GioHangDTO>();
                list = (List<GioHangDTO>)Session["Cart"];
                GioHangBUS bus=new GioHangBUS();

                if (bus.KiemTra(list, MaSP) == true)
                {
                    lbTrangThai.Text = "Sản phẩm đã có trong giỏ hàng, nếu muốn mua thêm hãy cập nhật số lượng !";
                    lbTrangThai.ForeColor = System.Drawing.Color.White;
                    lbTrangThai.BackColor = System.Drawing.Color.Red;
                }
                else
                {

                    list = bus.ThemSanPhamGioHang(list, MaSP);
                    Session["Cart"] = list;
                }
            }

            Session["url"] = Request.UrlReferrer;
            

            if (Request.QueryString["delid"] != null)
            {
                //Lấy ConString
                int MaSP = int.Parse(Request.QueryString["delid"].ToString());
                List<GioHangDTO> list = new List<GioHangDTO>();
                list = (List<GioHangDTO>)Session["Cart"];
                Session["Cart"] = new GioHangBUS().XoaSanPhamGioHang(list, MaSP);
            }

            if (Request.QueryString["error"] != null)
            {
                //Lấy ConString
                String MaLoi= Request.QueryString["error"].ToString();

                lbTrangThai.Text = "Lỗi nhập số lượng các ID sau : <b>" + MaLoi+" </b> ... Vui lòng nhập số lượng > 0";
                lbTrangThai.ForeColor = System.Drawing.Color.White;
                lbTrangThai.BackColor = System.Drawing.Color.Red;
                
            }
        }

        LoadGioHang();

        link.NavigateUrl = Session["url"].ToString();

    }

    public void LoadGioHang()
    {
        List<GioHangDTO> list = new List<GioHangDTO>();
        list = (List < GioHangDTO >)Session["Cart"];

        ShopCart.DataSource = list;
        ShopCart.DataBind();

        lbTongGiaTri.Text = new GioHangBUS().TinhTongTienGioHang(list).ToString();
        lbTongGiaTri2.Text = lbTongGiaTri.Text;

        if (list.Count == 0)
        {
            lbTrangThai.Text = "Giỏ hàng rỗng !!!";
        }
    }




    protected void btnCapNhat_Click(object sender, EventArgs e)
    {
        List<GioHangDTO>list=new List<GioHangDTO>();
        list=(List<GioHangDTO>)Session["Cart"];

        int i = 2;
        int j = 1;
        int MaSP;
        Double SoLuong;
        String TenControl;

        String error = "";

        while (j <= list.Count)
        {
            TenControl = Request.Form.AllKeys[i++].ToString();
            MaSP = int.Parse(Request.Form[TenControl].ToString());
            TenControl = Request.Form.AllKeys[i++].ToString();
            SoLuong = Double.Parse(Request.Form[TenControl].ToString());

            if (SoLuong < 0)
            {
                error = error + MaSP + "_";
            }
            else
            {
                list = new GioHangBUS().CapNhatGioHang(list, MaSP, SoLuong);
            }

            j++;
        }

        Session["Cart"] = list;

        if (error != "")
        {
            Response.Redirect("Cart.aspx?error=" + error);
        }
        else
        {
            Response.Redirect("Cart.aspx");
        }
    }
    protected void btnThanhToan_Click(object sender, EventArgs e)
    {
        Response.Redirect("Pay.aspx");
    }
}